package com.tpc.control.jpa;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * SaleData entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "sale_data", catalog = "production_control")
public class SaleData implements java.io.Serializable {

	// Fields

	private SaleDataId id;
	private Company company;
	private Division division;
	private String customer;
	private String customerName;
	private String customerPoNo;
	private String saleOrg;
	private String distrChannel;
	private String plant;
	private String material;
	private Date dueDate;
	private Double orderAmount;
	private Double completeAmount;
	private Double remainAmount;
	private String endUserCode;
	private String endUserName;
	private String completeFlag;
	private Double quantity;
	private String uom;
	private Double price;
	private String currency;
	private Set<CommonData> commonDatas = new HashSet<CommonData>(0);
	private Set<ReserveItemSale> reserveItemSales = new HashSet<ReserveItemSale>(
			0);
	private Set<PlanGreige> planGreiges = new HashSet<PlanGreige>(0);

	// Constructors

	/** default constructor */
	public SaleData() {
	}

	/** minimal constructor */
	public SaleData(SaleDataId id, Company company, String customer,
			String customerName, String customerPoNo, String saleOrg,
			String distrChannel, String plant, String material,
			Double orderAmount, Double completeAmount, Double remainAmount,
			String endUserCode, String endUserName, String completeFlag,
			Double quantity, String uom, Double price, String currency) {
		this.id = id;
		this.company = company;
		this.customer = customer;
		this.customerName = customerName;
		this.customerPoNo = customerPoNo;
		this.saleOrg = saleOrg;
		this.distrChannel = distrChannel;
		this.plant = plant;
		this.material = material;
		this.orderAmount = orderAmount;
		this.completeAmount = completeAmount;
		this.remainAmount = remainAmount;
		this.endUserCode = endUserCode;
		this.endUserName = endUserName;
		this.completeFlag = completeFlag;
		this.quantity = quantity;
		this.uom = uom;
		this.price = price;
		this.currency = currency;
	}

	/** full constructor */
	public SaleData(SaleDataId id, Company company, Division division,
			String customer, String customerName, String customerPoNo,
			String saleOrg, String distrChannel, String plant, String material,
			Date dueDate, Double orderAmount, Double completeAmount,
			Double remainAmount, String endUserCode, String endUserName,
			String completeFlag, Double quantity, String uom, Double price,
			String currency, Set<CommonData> commonDatas,
			Set<ReserveItemSale> reserveItemSales, Set<PlanGreige> planGreiges) {
		this.id = id;
		this.company = company;
		this.division = division;
		this.customer = customer;
		this.customerName = customerName;
		this.customerPoNo = customerPoNo;
		this.saleOrg = saleOrg;
		this.distrChannel = distrChannel;
		this.plant = plant;
		this.material = material;
		this.dueDate = dueDate;
		this.orderAmount = orderAmount;
		this.completeAmount = completeAmount;
		this.remainAmount = remainAmount;
		this.endUserCode = endUserCode;
		this.endUserName = endUserName;
		this.completeFlag = completeFlag;
		this.quantity = quantity;
		this.uom = uom;
		this.price = price;
		this.currency = currency;
		this.commonDatas = commonDatas;
		this.reserveItemSales = reserveItemSales;
		this.planGreiges = planGreiges;
	}

	// Property accessors
	@EmbeddedId
	@AttributeOverrides( {
			@AttributeOverride(name = "saleOrder", column = @Column(name = "sale_order", nullable = false, length = 20)),
			@AttributeOverride(name = "saleItem", column = @Column(name = "sale_item", nullable = false, length = 6)) })
	public SaleDataId getId() {
		return this.id;
	}

	public void setId(SaleDataId id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "company_id", nullable = false)
	public Company getCompany() {
		return this.company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "division")
	public Division getDivision() {
		return this.division;
	}

	public void setDivision(Division division) {
		this.division = division;
	}

	@Column(name = "customer", nullable = false, length = 10)
	public String getCustomer() {
		return this.customer;
	}

	public void setCustomer(String customer) {
		this.customer = customer;
	}

	@Column(name = "customer_name", nullable = false, length = 40)
	public String getCustomerName() {
		return this.customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	@Column(name = "customer_po_no", nullable = false, length = 35)
	public String getCustomerPoNo() {
		return this.customerPoNo;
	}

	public void setCustomerPoNo(String customerPoNo) {
		this.customerPoNo = customerPoNo;
	}

	@Column(name = "sale_org", nullable = false, length = 4)
	public String getSaleOrg() {
		return this.saleOrg;
	}

	public void setSaleOrg(String saleOrg) {
		this.saleOrg = saleOrg;
	}

	@Column(name = "distr_channel", nullable = false, length = 2)
	public String getDistrChannel() {
		return this.distrChannel;
	}

	public void setDistrChannel(String distrChannel) {
		this.distrChannel = distrChannel;
	}

	@Column(name = "plant", nullable = false, length = 4)
	public String getPlant() {
		return this.plant;
	}

	public void setPlant(String plant) {
		this.plant = plant;
	}

	@Column(name = "material", nullable = false, length = 18)
	public String getMaterial() {
		return this.material;
	}

	public void setMaterial(String material) {
		this.material = material;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "due_date", length = 10)
	public Date getDueDate() {
		return this.dueDate;
	}

	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}

	@Column(name = "order_amount", nullable = false, precision = 22, scale = 0)
	public Double getOrderAmount() {
		return this.orderAmount;
	}

	public void setOrderAmount(Double orderAmount) {
		this.orderAmount = orderAmount;
	}

	@Column(name = "complete_amount", nullable = false, precision = 22, scale = 0)
	public Double getCompleteAmount() {
		return this.completeAmount;
	}

	public void setCompleteAmount(Double completeAmount) {
		this.completeAmount = completeAmount;
	}

	@Column(name = "remain_amount", nullable = false, precision = 22, scale = 0)
	public Double getRemainAmount() {
		return this.remainAmount;
	}

	public void setRemainAmount(Double remainAmount) {
		this.remainAmount = remainAmount;
	}

	@Column(name = "end_user_code", nullable = false, length = 25)
	public String getEndUserCode() {
		return this.endUserCode;
	}

	public void setEndUserCode(String endUserCode) {
		this.endUserCode = endUserCode;
	}

	@Column(name = "end_user_name", nullable = false, length = 40)
	public String getEndUserName() {
		return this.endUserName;
	}

	public void setEndUserName(String endUserName) {
		this.endUserName = endUserName;
	}

	@Column(name = "complete_flag", nullable = false, length = 1)
	public String getCompleteFlag() {
		return this.completeFlag;
	}

	public void setCompleteFlag(String completeFlag) {
		this.completeFlag = completeFlag;
	}

	@Column(name = "quantity", nullable = false, precision = 22, scale = 0)
	public Double getQuantity() {
		return this.quantity;
	}

	public void setQuantity(Double quantity) {
		this.quantity = quantity;
	}

	@Column(name = "uom", nullable = false, length = 6)
	public String getUom() {
		return this.uom;
	}

	public void setUom(String uom) {
		this.uom = uom;
	}

	@Column(name = "price", nullable = false, precision = 22, scale = 0)
	public Double getPrice() {
		return this.price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	@Column(name = "currency", nullable = false, length = 10)
	public String getCurrency() {
		return this.currency;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "saleData")
	public Set<CommonData> getCommonDatas() {
		return this.commonDatas;
	}

	public void setCommonDatas(Set<CommonData> commonDatas) {
		this.commonDatas = commonDatas;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "saleData")
	public Set<ReserveItemSale> getReserveItemSales() {
		return this.reserveItemSales;
	}

	public void setReserveItemSales(Set<ReserveItemSale> reserveItemSales) {
		this.reserveItemSales = reserveItemSales;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "saleData")
	public Set<PlanGreige> getPlanGreiges() {
		return this.planGreiges;
	}

	public void setPlanGreiges(Set<PlanGreige> planGreiges) {
		this.planGreiges = planGreiges;
	}

	/* Customize */
	public List<CommonData> retrieveCommonDatasList() {
//		System.out.println("commonDatasList");
		List<CommonData>  dataList = new ArrayList();
		Iterator item = this.commonDatas.iterator();
		while (item.hasNext()) {
			CommonData data = (CommonData)item.next();
			if (data.getId().getSeq().equals("0")) {
				dataList.add(data);
			}
		}
		return dataList;
	}

	public List<CommonData> retrieveCommonDatasListAll() {
/*		List<CommonData>  dataList = new ArrayList();
		Iterator item = this.commonDatas.iterator();
		while (item.hasNext()) {
			CommonData data = (CommonData)item.next();
			dataList.add(data);
		}*/
		List<CommonData> dataList = new ArrayList<CommonData>(this.getCommonDatas());
		return dataList;
	}
}